//
//  StaticData.m
//  HeWeiZhiNeng
//
//  Created by chengzhang Yan on 2017/12/1.
//  Copyright © 2017年 YanCeyMichael.github.io. All rights reserved.
//

#import "StaticData.h"
#define settings [NSUserDefaults standardUserDefaults]

@implementation StaticData

#pragma mark ---- core private function

+ (void)saveObj:(id)obj WithKey:(NSString *)key
{
    [settings removeObjectForKey:key];
    if ([obj isKindOfClass:NSString.class] || [obj isKindOfClass:NSNumber.class]) {
        [settings setObject:obj forKey:key];
    }else {
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:obj];
        [settings setObject:data forKey:key];
    }
    [settings synchronize];
}

+ (id)unarchiveObjWithKey:(NSString *)key
{
    id obj = nil;
    @try
    {
        obj = [NSKeyedUnarchiver unarchiveObjectWithData:[settings objectForKey:key]];
    }
    @catch (NSException *exception)
    {
        debug_NSLog(@"get %@ exception",key);
    }
    @finally
    {
        
    }
    return obj;
}

#pragma mark ---- public function

+ (void)saveToken:(NSString*)token
{
    [StaticData saveObj:token WithKey:@"token"];
}

+ (NSString*)getToken
{
    NSString *token = [settings stringForKey:@"token"];
    return token;
}

+ (void)clearAllData
{
    [settings removeObjectForKey:@"token"];
    [settings synchronize];
}

@end
